/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.tcc.view;

import br.com.tcc.dao.DAOContasBancarias;
import br.com.tcc.item.view.ContasBancariasItemView;
import br.com.tcc.modelo.ContasBancarias;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

/**
 * FXML Controller class
 *
 * @author Edidelson
 */
public class ContasBancariaController implements Initializable, ControlledScreen {

    private ObservableList<ContasBancariasItemView> list = FXCollections.observableArrayList();

    ScreensController myController;
    @FXML
    public AnchorPane contentPane;
    @FXML
    ComboBox cbTipo = new ComboBox();
    @FXML
    TextField txtCodigo = new TextField();
    @FXML
    TextField txtDescricao = new TextField();
    @FXML
    private TableColumn colCodigo;
    @FXML
    private TableColumn colDescricao;
    @FXML
    private TableColumn colTipo;
    @FXML
    private TableView tbContas;

    private ContasBancarias contaBancaria;

    /**
     * Initializes the controller class.
     *
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        cellColumns();
        iniciar();
    }

    @Override
    public void setScreenParent(ScreensController screenPage) {
        myController = screenPage;
    }

    @FXML
    private void adicionar(ActionEvent event) {
        if (contaBancaria == null) {
            contaBancaria = new ContasBancarias();
            contaBancaria.setTipo((String) cbTipo.getSelectionModel().getSelectedItem());
            contaBancaria.setDescricao(txtDescricao.getText());
            DAOContasBancarias dao = new DAOContasBancarias();
            dao.adicionar(contaBancaria);
            iniciar();
            limpar();
        }
    }

    @FXML
    private void alterar(ActionEvent event) {
        if (contaBancaria != null) {
            contaBancaria.setCodigo(Integer.valueOf(txtCodigo.getText()));
            contaBancaria.setTipo((String) cbTipo.getSelectionModel().getSelectedItem());
            contaBancaria.setDescricao(txtDescricao.getText());
            DAOContasBancarias dao = new DAOContasBancarias();
            dao.alterar(contaBancaria);
            iniciar();
            limpar();
        }
    }

    @FXML
    private void remover(ActionEvent event) {
        if (contaBancaria != null) {
            DAOContasBancarias dao = new DAOContasBancarias();
            dao.remover(contaBancaria);
            iniciar();
            limpar();
        }
    }

    public void limpar() {
        txtCodigo.setText("");
        txtDescricao.setText("");
        cbTipo.getSelectionModel().select(0);
        contaBancaria = null;
    }

    public void cellColumns() {
        colCodigo.setCellValueFactory(new PropertyValueFactory("codigo"));
        colDescricao.setCellValueFactory(new PropertyValueFactory("descricao"));
        colTipo.setCellValueFactory(new PropertyValueFactory("tipoConta"));
    }

    private void iniciar() {
        tbContas.getItems().clear();
        DAOContasBancarias dao = new DAOContasBancarias();
        List<ContasBancarias> contasBancariases = dao.getLista();
        contasBancariases.stream().map((contasBancarias) -> new ContasBancariasItemView(contasBancarias)).forEach((itemView) -> {
            tbContas.getItems().add(itemView);
        });
    }

    @FXML
    private void preencherCampos(MouseEvent event) {
        ContasBancariasItemView itemView = (ContasBancariasItemView) tbContas.getSelectionModel().getSelectedItem();
        if (itemView != null) {
            contaBancaria = new DAOContasBancarias().consultar(Integer.parseInt(itemView.getCodigo()));
            contaBancaria.setCodigo(Integer.parseInt(itemView.getCodigo()));
            contaBancaria.setDescricao(itemView.getDescricao());
            contaBancaria.setTipo(itemView.getTipoConta());
            txtDescricao.setText(itemView.getDescricao());
            cbTipo.getSelectionModel().select(itemView.getTipoConta());
            txtCodigo.setText(itemView.getCodigo());
        }
    }
}
